<?php

namespace app\api\service;

use app\api\model\Address;
use app\api\model\Order;
use app\api\model\User;
use app\Response;

class OrderService
{
    public static function orderEditService($uid,$post_num,$state = 2){
        $userInfo = User::userInfo(["uid"=>$uid]);
        if(empty($userInfo)){
            return Response::wrapError(-200,'非法请求');
        }
        if($userInfo['type'] != 15){
            return Response::wrapError(-200,'账户非快递手机端!');
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError('此账户已被禁用,如有疑问请联系客服人员',-200);
        }
        $where[] = ["paid","=",$userInfo["paid"]];
        if(ctype_digit($post_num)){
            $o_number = $post_num;
            $prefix = "";
            $where[] = ["o_number","=",$o_number];
        }else{
            preg_match_all('/\d+/', $post_num, $numbers); // 匹配数字
            preg_match_all('/[A-Za-z]+/', $post_num, $letters); // 匹配字母
            if(!empty($numbers)){
                $o_number = $numbers[0][0];
                $where[] = ["o_number","=",$o_number];
            }
            if(!empty($letters)){
                $prefix = $letters[0][0];
                $where[] = ["prefix","=",$prefix];
            }
        }
        $orderInfo = Order::dataView($where);
        if(empty($orderInfo)){
            return Response::wrapError(2,'订单不存在');
        }
        $up_data = [
            "state" => $state
        ];
        $res = Order::dataEdit(["id"=>$orderInfo["id"],"o_number"=>$o_number],$up_data);
        if($res){
            return Response::wrapData();
        }else{
            return Response::wrapError(0,"操作失败");
        }
    }


    public static function orderAddService($uid,$post_num,$a_id){
        $userInfo = User::userInfo(["uid"=>$uid]);
        if(empty($userInfo)){
            return Response::wrapError(-200,'非法请求');
        }
        if($userInfo['type'] != 15){
            return Response::wrapError(-200,'账户非快递手机端!');
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError('此账户已被禁用,如有疑问请联系客服人员',-200);
        }
        if(ctype_digit($post_num)){
            $o_number = $post_num;
            $prefix = "";
            $where[] = ["o_number","=",$o_number];
        }else{
            preg_match_all('/\d+/', $post_num, $numbers); // 匹配数字
            preg_match_all('/[A-Za-z]+/', $post_num, $letters); // 匹配字母
            if(!empty($numbers)){
                $o_number = $numbers[0][0];
                $where[] = ["o_number","=",$o_number];
            }
            if(!empty($letters)){
                $prefix = $letters[0][0];
                $where[] = ["prefix","=",$prefix];
            }

        }
        $orderInfo = Order::dataView($where);
        if(!empty($a_id)){
            $addressInfo = Address::addressInfo(["id"=>$a_id]);
            $m_uid = $addressInfo["uid"];
        }else{
            $addressInfo = [];
            $m_uid = 0;
        }
        if($orderInfo){
            $data = [
                "uid" => $m_uid,
                "paid" => $userInfo["paid"],
                "o_number" => $o_number,
                "prefix" => $prefix,
                "state" => 2,
                "addtime" => time(),
                "address_info" => $addressInfo,
                "significant" => $addressInfo["significant"]
            ];
            $res = Order::dataEdit(["id"=>$orderInfo["id"],"o_number" => $o_number],$data);
        }else{
            $data = [
                "uid" => $m_uid,
                "paid" => $userInfo["paid"],
                "o_number" => $o_number,
                "prefix" => $prefix,
                "state" => 2,
                "addtime" => time(),
                "address_info" => $addressInfo,
                "significant" => $addressInfo["significant"]
            ];
            $res = Order::dataAdd($data);
        }

        if($res){
            return Response::wrapData();
        }else{
            return Response::wrapError(0,"添加失败!");
        }
    }



    public static function addressListService($uid,$province,$city,$area,$street){
        $userInfo = User::userInfo(["uid"=>$uid]);
        if(empty($userInfo)){
            return Response::wrapError(-200,'非法请求');
        }
        if($userInfo['type'] != 15){
            return Response::wrapError(-200,'账户非快递手机端!');
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError('此账户已被禁用,如有疑问请联系客服人员',-200);
        }
        $where[] = ["paid","=",$userInfo["paid"]];
        if(!empty($province)){
            $province = trim($province);
            $where[] = ["province","like","%".$province."%"];
        }
        if(!empty($city)){
            $city = trim($city);
            $where[] = ["city","like","%".$city."%"];
        }
        if(!empty($area)){
            $area = trim($area);
            $where[] = ["area","like","%".$area."%"];
        }
        if(!empty($street)){
            $street = trim($street);
            $where[] = ["street","like","%".$street."%"];
        }
        $list = Address::datalist($where,1,150);
        if($list){
            return Response::wrapData($list);
        }else{
            $list = [];
            return Response::wrapData($list);
        }
    }
}